package com.example.rydwlib.fragment


import androidx.fragment.app.Fragment
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.baselib.fragment.BaseFragment

import com.example.rydwlib.R
import com.example.rydwlib.RydwViewModel
import com.example.rydwlib.adapter.*
import kotlinx.android.synthetic.main.fragment_rydw_alarm.*
import java.lang.Exception

/**
 * A simple [Fragment] subclass.
 */
class RydwAlarmFragment : BaseFragment<RydwViewModel>() {
    private val overTimeAdapter: RyOverTimeAdapter by lazy { RyOverTimeAdapter(R.layout.ry_overtime_item) }
    private val overPersonAdapter: RyOverPersonAdapter by lazy { RyOverPersonAdapter(R.layout.ry_overperson_item) }
    private val personCallAdapter: RyPersonCallAdapter by lazy { RyPersonCallAdapter(R.layout.ry_personcall_item) }
    private val specialAlarmAdapter: RySpecialAlarmAdapter by lazy { RySpecialAlarmAdapter(R.layout.ry_specialalarm_item) }
    private val sysAlarmAdapter: RySysAlarmAdapter by lazy { RySysAlarmAdapter(R.layout.ry_sysalarm_item) }
    override fun providerVMClass(): Class<RydwViewModel>? = RydwViewModel::class.java

    override fun contentLayout(): Int = R.layout.fragment_rydw_alarm

    override fun initView() {
        ry_alarm_bottom.itemIconTintList = null
        ry_alarm_srl.setOnRefreshListener {
            viewModel.isLoad = false
            viewModel.loadRyAlarmData()
        }


//        overTimeAdapter.emptyView =
//            LayoutInflater.from(context).inflate(R.layout.empty_item, null, false)
//        overPersonAdapter.emptyView =
//            LayoutInflater.from(context).inflate(R.layout.empty_item, null, false)
//        personCallAdapter.emptyView =
//            LayoutInflater.from(context).inflate(R.layout.empty_item, null, false)
//        specialAlarmAdapter.emptyView =
//            LayoutInflater.from(context).inflate(R.layout.empty_item, null, false)
//        sysAlarmAdapter.emptyView =
//            LayoutInflater.from(context).inflate(R.layout.empty_item, null, false)
        ry_alarm_rcy.layoutManager =
            LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        ry_alarm_rcy.adapter = overTimeAdapter
        ry_alarm_bottom.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.over_time -> {
                    if (ry_alarm_rcy.adapter != overTimeAdapter) ry_alarm_rcy.adapter =
                        overTimeAdapter
                    return@setOnNavigationItemSelectedListener true
                }
                R.id.over_person -> {
                    if (ry_alarm_rcy.adapter != overPersonAdapter) ry_alarm_rcy.adapter =
                        overPersonAdapter
                    return@setOnNavigationItemSelectedListener true
                }
                R.id.call_alarm -> {
                    if (ry_alarm_rcy.adapter != personCallAdapter) ry_alarm_rcy.adapter =
                        personCallAdapter
                    return@setOnNavigationItemSelectedListener true
                }
                R.id.special_alarm -> {
                    if (ry_alarm_rcy.adapter != specialAlarmAdapter) ry_alarm_rcy.adapter =
                        specialAlarmAdapter
                    return@setOnNavigationItemSelectedListener true
                }
                R.id.sys_alarm -> {
                    if (ry_alarm_rcy.adapter != sysAlarmAdapter) ry_alarm_rcy.adapter =
                        sysAlarmAdapter
                    return@setOnNavigationItemSelectedListener true
                }
                else -> return@setOnNavigationItemSelectedListener false
            }
        }

    }

    override fun initData() {
        if (!viewModel.getUnitInfo().isNullOrEmpty()){
            ry_alarm_nickname.text=viewModel.getUnitInfo()["unitname"]
        }
        viewModel.getAlarmData().observe(this, Observer {
            overTimeAdapter.setNewData(it.overtimealarm)
            overPersonAdapter.setNewData(it.overpersonalarm)
            personCallAdapter.setNewData(it.personcallalarm)
            specialAlarmAdapter.setNewData(it.specialpersonabnorma)
            sysAlarmAdapter.setNewData(it.sysalarm)
            ry_alarm_empty.visibility=View.INVISIBLE
        })
    }

    override fun requestFinally(it: Int?) {
        super.requestFinally(it)
        ry_alarm_srl.isRefreshing = false

    }
    override fun requestError(it: Exception?) {
        ry_alarm_empty.visibility=View.VISIBLE
        if (it?.message=="暂无数据"){
            ry_alarm_empty.findViewById<TextView>(R.id.err_txt).text=it?.message
            ry_alarm_empty.findViewById<TextView>(R.id.err_msg).visibility=View.INVISIBLE
//            ry_alarm_empty.findViewById<ImageView>(R.id.err_img).visibility=View.INVISIBLE
        }else{
            super.requestError(it)
            ry_alarm_empty.findViewById<TextView>(R.id.err_msg).text=it?.message
        }
        ry_alarm_empty.findViewById<Button>(R.id.err_rest_btn).setOnClickListener {  viewModel.loadRyAlarmData() }
    }

}
